package cn.itsource.service.impl;

import cn.itsource.domain.Param;
import cn.itsource.mapper.ParamMapper;
import cn.itsource.query.Query;
import cn.itsource.service.ParamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ParamServiceImpl implements ParamService {


    @Autowired
    private ParamMapper paramMapper;


    @Override
    public List<Param> findAll() {
        return paramMapper.findAll();
    }

    @Override
    public Param findOne(Long id) {
        return paramMapper.findOne(id);
    }

    @Override
    public void add(Param param) {
        paramMapper.add(param);
    }

    @Override
    public void update(Param param) {
         param.setUpdateTime(new Date());
        paramMapper.update(param);
    }

    @Override
    public void delete(Long id) {
        paramMapper.delete(id);
    }

    //分页+模糊查询
    @Override
    public PageInfo<Param> pageQuery(Query query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Param> paramList = paramMapper.pageQuery(query);
        return new PageInfo<>(paramList);
    }

    //批量删除
    @Override
    public void patchdel(Long[] ids) {
        paramMapper.patchdel(ids);
    }


    //侧边栏展示数据
    @Override
    public Map<String, String> getParams() {
        Map<String, String> map = new HashMap<>();
        List<Param> paramList = paramMapper.findAll();
        for (Param param : paramList) {
            map.put(param.getParamKey(),param.getParamValue());
        }
        return map;
    }
}
